package com.qianfeng.bob.xiexiaobo.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.qianfeng.bob.xiexiaobo.R;
import com.qianfeng.bob.xiexiaobo.application.MyApplication;
import com.qianfeng.bob.xiexiaobo.bean.HomeFragmentDatas;
import com.qianfeng.bob.xiexiaobo.cache.MyCache;

import java.util.List;

public class MatchContentAdapter extends RecyclerView.Adapter<MatchContentAdapter.ViewHoladerA>{
	private List<HomeFragmentDatas.DataEntity.ItemsEntity> datas;
	private Context context;
	private List<Integer> heights;
	private int count = 0;
	private RecyclerView recyclerView;
	private RequestQueue mRequestQueue;
	private MyCache mCache;
	


	public MatchContentAdapter(List<HomeFragmentDatas.DataEntity.ItemsEntity> datas, Context context , List<Integer> heights, RecyclerView recyclerView) {
		this.datas = datas;
		this.context = context;
		this.heights = heights;
		this.recyclerView = recyclerView;
		mCache = ((MyApplication) ((Activity) context).getApplication()).getMyCache();
		mRequestQueue =((MyApplication) ((Activity) context).getApplication()).getRequestQueue();
	}

	@Override
	public ViewHoladerA onCreateViewHolder(ViewGroup parent, int position) {
		count ++;
		ViewHoladerA holader;
		View view = LayoutInflater.from(context).inflate(R.layout.item_demo, parent, false);
		holader = new ViewHoladerA(view);
		return holader;
	}

	@Override
	public void onBindViewHolder(ViewHoladerA holder, int position) {
		ViewGroup.LayoutParams params =  holder.itemView.getLayoutParams();
		params.height = heights.get(position);
		holder.itemView.setLayoutParams(params);
        holder.imageView.setTag(datas.get(position).getComponent().getPicUrl());
		loadImage(holder.imageView);
	}

	@Override
	public int getItemCount() {
		return datas.size();
	}
	
	
	class ViewHoladerA extends RecyclerView.ViewHolder{
		private ImageView imageView;

		public ViewHoladerA(View view) {
			super(view);
			imageView = (ImageView) view.findViewById(R.id.iv_item_list);
		}
		
	}

	private void loadImage(ImageView imageView){
		String url = ((String) imageView.getTag());
		ImageLoader loader = new ImageLoader(mRequestQueue,mCache);
		ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView,R.mipmap.bottom_comment_input,R.mipmap.bottom_comment_input);
		loader.get(url,listener);
	}


}
